package com.shaokel.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shaokel.eduservice.entity.EduSubject;
import com.shaokel.eduservice.entity.excel.SubjectData;
import com.shaokel.eduservice.entity.subject.OneSubject;
import com.shaokel.eduservice.entity.subject.TwoSubject;
import com.shaokel.eduservice.listener.SubjectExcelListener;
import com.shaokel.eduservice.mapper.EduSubjectMapper;
import com.shaokel.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程科目 服务实现类
 *
 * @author 少丶客
 * @since 2020-06-02
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //课程分类列表（树形）
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1.获取一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        //2.获取二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //创建最终集合finalSubjectList，把一级分类放到最终集合中
        List<OneSubject> oneFinalSubjectList = new ArrayList<>();

        /*
        * 3.封装一级分类
        * 遍历一级分类，把结果放到finalSubjectList中
        * */
        for (int i = 0; i < oneSubjectList.size();i++){
            //取出每个一级分类的对象
            EduSubject eduSubject = oneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
            //把每个一级分类的对象的值放到OneSubject
            //方法1：
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            //方法2：   把eduSubject复制到oneSubject
            BeanUtils.copyProperties(eduSubject,oneSubject);
            //获得值的一级分类封装到finalSubjectList
            oneFinalSubjectList.add(oneSubject);

            //4.封装二级分类
            //创建twoFinalSubjectList，把二级分类放到twoFinalSubjectList中
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();

            for (int m = 0; m < twoSubjectList.size(); m++) {
                //获取每个二级分类
                EduSubject tSubject = twoSubjectList.get(m);
                //判断二级分类的id是否与一级分类的id相同
                if(tSubject.getParentId().equals(oneSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    //把tSubject复制到twoSubject
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    //获得值的一级分类封装到finalSubjectList
                    twoFinalSubjectList.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoFinalSubjectList);
        }
        return oneFinalSubjectList;
    }

    //添加课程分类
    @Override
    public void savaSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try { 
            //文件输入流
            InputStream inputStream = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(inputStream, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
